package com.DreamTeam.daoImpl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import sun.print.PeekGraphics;

import com.DreamTeam.dao.IOrderMasterDao;
import com.DreamTeam.po.OrderMaster;

public class OrderMasterDao implements IOrderMasterDao {
	private Connection conn = null;

	public OrderMasterDao(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean doAdd(OrderMaster orderMaster) {
		String sql = "insert into orderMaster values(?,?,?,?,?,?)";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, orderMaster.getOrderNo());
			prsmt.setString(2, orderMaster.getCustomerNo());
			prsmt.setString(3, orderMaster.getEmployeeNo());
			prsmt.setDate(4, new java.sql.Date(orderMaster.getOrderDate()
					.getTime()));
			prsmt.setDouble(5, orderMaster.getOrderSum());
			prsmt.setString(6, orderMaster.getInvoiceNo());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		return false;
	}

	@Override
	public boolean doDelete(OrderMaster orderMaster) {
		String sql = "delete from orderMaster where OrderNo=?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, orderMaster.getOrderNo());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public boolean doUpdate(OrderMaster orderMaster) {
		String sql = "update orderMaster set orderNo=?,customerNo=?"
				+ ",employeeNo=?,orderDate=?,orderSum=?,InvoiceNo=? where orderNo=?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, orderMaster.getOrderNo());
			prsmt.setString(2, orderMaster.getCustomerNo());
			prsmt.setString(3, orderMaster.getEmployeeNo());
			prsmt.setDate(4, new java.sql.Date(orderMaster.getOrderDate()
					.getTime()));
			prsmt.setDouble(5, orderMaster.getOrderSum());
			prsmt.setString(6, orderMaster.getInvoiceNo());
			prsmt.setString(7, orderMaster.getOrderNo());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public List<OrderMaster> queryAll() {
		String sql = "select * from orderMaster";
		List<OrderMaster> list = new ArrayList();
		PreparedStatement prsmt = null;
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderMaster om = new OrderMaster();
				om.setOrderNo(re.getString(1));
				om.setCustomerNo(re.getString(2));
				om.setEmployeeNo(re.getString(3));
				om.setOrderDate(re.getDate(4));
				om.setOrderSum(re.getDouble(5));
				om.setInvoiceNo(re.getString(6));
				list.add(om);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public List<OrderMaster> queryOrderMasterByColumn(String keyword,String columnName,String orderColumn,String model) {
		String sql = "select * from orderMaster where "+columnName+" like ? order by "+orderColumn+" "+model;
		PreparedStatement prsmt = null;
		List<OrderMaster> list = new ArrayList();
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderMaster om = new OrderMaster();
				om.setOrderNo(re.getString(1));
				om.setCustomerNo(re.getString(2));
				om.setEmployeeNo(re.getString(3));
				om.setOrderDate(re.getDate(4));
				om.setOrderSum(re.getDouble(5));
				om.setInvoiceNo(re.getString(6));
				list.add(om);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<OrderMaster> queryOrderByColumnName(String columnName,String model) {
		String sql = "select * from orderMaster order by "+columnName+" "+model;
		List<OrderMaster> list = new ArrayList();
		PreparedStatement prsmt = null;
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderMaster om = new OrderMaster();
				om.setOrderNo(re.getString(1));
				om.setCustomerNo(re.getString(2));
				om.setEmployeeNo(re.getString(3));
				om.setOrderDate(re.getDate(4));
				om.setOrderSum(re.getDouble(5));
				om.setInvoiceNo(re.getString(6));
				list.add(om);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<OrderMaster> queryByKeyword(String keyword) {
		String sql = "select * from orderMaster where orderNo like ? or customerNo like ? " +
				"or employeeNo like ? or orderDate like ? or orderSum like ? or invoiceNo like ?";
		PreparedStatement prsmt = null;
		List<OrderMaster> list = new ArrayList();
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			prsmt.setString(2, "%"+keyword+"%");
			prsmt.setString(3, "%"+keyword+"%");
			prsmt.setString(4, "%"+keyword+"%");
			prsmt.setString(5, "%"+keyword+"%");
			prsmt.setString(6, "%"+keyword+"%");
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				OrderMaster om = new OrderMaster();
				om.setOrderNo(re.getString(1));
				om.setCustomerNo(re.getString(2));
				om.setEmployeeNo(re.getString(3));
				om.setOrderDate(re.getDate(4));
				om.setOrderSum(re.getDouble(5));
				om.setInvoiceNo(re.getString(6));
				list.add(om);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	

}
